package com.yuedao.sender.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuedao.sender.model.RatedDriver;
import com.yuedao.sender.model.RatedDriverPunish;
import com.yuedao.sender.model.RatedPassenger;
import com.yuedao.sender.model.RatedPassengerComplaint;
import com.yuedao.sender.service.RatedDriverPunishRepository;
import com.yuedao.sender.service.RatedDriverRepository;
import com.yuedao.sender.service.RatedPassengerComplaintRepository;
import com.yuedao.sender.service.RatedPassengerRepository;
import com.yuedao.sender.service1.RateService;
import com.yuedao.sender.util.AppLogger;

/**
 * 
 * @author xfm
 * 2018年5月16日
 */
@RestController
@RequestMapping("/rated")
public class RateController {

	@Autowired
	RateService rates;
	
	@Autowired
	RatedPassengerRepository ratedPassengerRepository;

	@Autowired
	RatedPassengerComplaintRepository ratedPassengerComplaintRepository;

	@Autowired
	RatedDriverPunishRepository ratedDriverPunishRepository;

	@Autowired
	RatedDriverRepository ratedDriverRepository;

	
	protected static AppLogger logger = AppLogger.getInstance();
	
	  @RequestMapping("/ratedDriver")
	    public String ratedDriver() {
		  List<RatedDriver> list = new ArrayList<RatedDriver>();
			String message = null;
			try {
				list = ratedDriverRepository.findBySendFlag(0);

				message=rates.sendRatedDriver(list);
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < list.size();i++){
					list.get(i).setSendFlag(1);
				}
				ratedDriverRepository.save(list);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
		
	    }


	    @RequestMapping("/ratedDriverPunish")
	    public String ratedDriverPunish() {
	    	List<RatedDriverPunish> list = new ArrayList<RatedDriverPunish>();
	    	String message = null;
			try {
				list = ratedDriverPunishRepository.findBySendFlag(0);

				message=rates.sendRatedDriverPunish(list);
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < list.size();i++){
					list.get(i).setSendFlag(1);
				}
				ratedDriverPunishRepository.save(list);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
	    }


	    @RequestMapping("/ratedPassenger")
	    public String ratedPassenger() {
	    	List<RatedPassenger> list = new ArrayList<RatedPassenger>();
	    	String message = null;
			try {
				list = ratedPassengerRepository.findBySendFlag(0);

				message=rates.sendRatedPassenger(list);
				
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < list.size();i++){
					list.get(i).setSendFlag(1);
				}
				ratedPassengerRepository.save(list);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
	    	
	    }


	    @RequestMapping("/ratedPassengerComplaint")
	    public String ratedPassengerComplaint() {
	    	List<RatedPassengerComplaint> list = new ArrayList<RatedPassengerComplaint>();
	    	String message = null;
			try {
				list = ratedPassengerComplaintRepository.findBySendFlag(0);

				message=rates.sendRatedPassengerComplaint(list);
				
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < list.size();i++){
					list.get(i).setSendFlag(1);
				}
				ratedPassengerComplaintRepository.save(list);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
	    }
	
	
}
